package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import lombok.Setter;
import Function.FCMutilPlayCache;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

//升级c装置的
public class FunctionUpgradeCDeviceShop extends AShopFuntion{
    private Vector<FixTank> needServiceNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;

    private int selectIndex = -1;
    public FunctionUpgradeCDeviceShop(){
        buildShopWindow();
    }

    public void buildShopWindow(){
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), gameSave.HOST_UID);
        if (tank != null && tank.getType().equals("owner")) {
            checkAndAdd(tank);
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if(link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                if (p != null) {
                    tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                    if (tank != null && tank.getType().equals("owner")) {
                        checkAndAdd(tank);
                    }
                }
            }
        }
        //还有牵引的
        FCPropulsion fc = fcv.findPropulsionByHostId(gameSave.HOST_UID);
        if( fc != null && fc.getPropulsionHostId().equals(gameSave.HOST_UID) &&fc.getPropulsionType().equals("tank")
                && ((FCTank)fc).getType().equals("owner")) {
            checkAndAdd((FCTank) fc);
        }

        setEndI(Math.min(needServiceNames.size(), showFristIndex + 3));
        resetUI();
    }

    private void checkAndAdd(FCTank tank) {
        FCItem item = FCRMaster.itemLib.getItemOfID(tank.getCdeviceID());
        if (item != null) {
            FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
            if(upgradeitem != null){//找到能升级的目标
                needServiceNames.add(new FixTank(tank.getCdeviceID(), tank, item.getUpgradeMoney(),
                        langLib.getNameOfId(tank.getName())+"-"+item.getChName()+"可升级到"+upgradeitem.getChName()));
            }
        }
    }

    private void resetUI(){
        FCRValues.resetUnifySayUIEntity();
        int m = 0;
        for (int i = showFristIndex; i < endI; i++) {
            FixTank fixTank = needServiceNames.get(i);
            String head = fixTank.text.substring(0, fixTank.text.indexOf("可"));
            String tail = fixTank.text.substring(fixTank.text.indexOf("可"));
            FCRValues.leftWindowUI[m].leftText = head;
            m++;
            FCRValues.leftWindowUI[m].rightText = tail+"   "+fixTank.needFixMoney+"G";
            m++;
        }
        if(needServiceNames.size() == 0){
            FCRValues.rightWindowUI[0].leftText = "你的战车没有可以升级的需要了";
        }
        FCRValues.rightWindowUI[2].leftText = "升级一下不会少你多少金币.";
        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "升级";
        FCRValues.rightWindowUI[6].rightText = "退出";
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if(entity.rightText.length() > 1 && needServiceNames.size() > 0) {
            selectIndex = showFristIndex + i/2;
            FixTank fixTank = needServiceNames.get(selectIndex);
            FCItem item = FCRMaster.itemLib.getItemOfID(fixTank.itemName);
            FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
            FCRValues.rightWindowUI[0].leftText = upgradeitem.getDetail();
            FCRValues.rightWindowUI[1].leftText = item.getDetail();

            String oldD = item.getChName()+" "+item.getWeight()+"t "+" 防"+item.getDefenseAdd()+" 特性:xxx";
            String newD = upgradeitem.getChName()+" "+upgradeitem.getWeight()+"t "+" 防"+upgradeitem.getDefenseAdd()+" 特性:xxx";
            FCRValues.rightWindowUI[0].leftText = newD;
            FCRValues.rightWindowUI[1].leftText = oldD;
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 3:
                if (showFristIndex >= 3) {
                    showFristIndex -= 3;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 3));
                    resetUI();
                }
                break;
            case 4:
                if (endI < needServiceNames.size()) {
                    showFristIndex += 3;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 3));
                    resetUI();
                }
                break;
            case 5:
                if(selectIndex >= 0){
                    FixTank fixTank = needServiceNames.get(selectIndex);
                    FCItem item = FCRMaster.itemLib.getItemOfID(fixTank.itemName);
                    FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
                    if(player1.getMoney() >= fixTank.needFixMoney){
                        fixTank.tank.setCdeviceID(gameSave.checkTankItem(upgradeitem.getName()));//生成新的克隆体
                        fixTank.tank.reInitShuxing();

                        //删除旧的克隆体
                        gameSave.delTankItemClone(fixTank.itemName);
                        player1.setMoney(player1.getMoney() - fixTank.needFixMoney);
                        needServiceNames.remove(selectIndex);
                        selectIndex = -1;
                        setEndI(Math.min(needServiceNames.size(), showFristIndex + 3));
                        resetUI();
                        FCRValues.rightWindowUI[1].leftText = "升级完毕，请继续";
                    } else {
                        FCRValues.rightWindowUI[2].leftText = "金币不足.";
                    }
                }
                break;
            case 6:
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }
}
